added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / samples / pigui / tk / tk.cs
blob01c919ad56585ce43c77e7b0e93dcdd1bc018b92
1 //------------------------------------------------------------------------------
2 // <copyright file="Tk.cs" company="Microsoft">
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 // </copyright>
14 //------------------------------------------------------------------------------
16 using System;
17 using System.Collections;
18 using System.Runtime.InteropServices;
20 namespace SharedSourceCLI.TK
22 public class TclException : Exception {
24 public TclException() {
28 public struct TclCmdToken {
29 internal IntPtr _token;
32 public delegate void TclAppInitProc(TclInterp interp);
33 public delegate void TclCmdProc(TclInterp interp, string[] argv);
34 public delegate void TclCmdDeleteProc();
36 public class TclInterp {
38 // innner unmanaged object
39 unsafe Tcl_Interp* _interp;
41 // hashtable to keep marshalled delegates alive
42 private Hashtable _keepAlive;
44 public TclInterp() {
45 unsafe {
46 _interp = TclNative.Tcl_CreateInterp();
47 if (_interp == null)
48 throw new OutOfMemoryException();
50 _keepAlive = new Hashtable();
53 internal void AddKeepAlive(Object o) {
54 _keepAlive.Add(o, null);
57 internal void RemoveKeepAlive(Object o) {
58 _keepAlive.Remove(o);
61 internal void HandleError(int res) {
62 if (res != TclNative.TCL_OK)
63 throw new TclException();
66 public void TclInit() {
67 unsafe {
68 HandleError(TclNative.Tcl_Init(_interp));
72 public void TkInit() {
73 unsafe {
74 HandleError(TkNative.Tk_Init(_interp));
78 public void TkMain(TclAppInitProc initProc, string[] argv) {
79 TclAppInitProcWrapper initWrapper = new TclAppInitProcWrapper(this, initProc);
81 unsafe {
82 TkNative.Tk_MainEx(argv.Length, argv, initWrapper.Callback, _interp);
85 GC.KeepAlive(initWrapper);
86 GC.KeepAlive(this);
89 public void TkMain(TclAppInitProc initProc) {
90 TkMain(initProc, Environment.GetCommandLineArgs());
93 public void Eval(string str) {
94 unsafe {
95 HandleError(TclNative.Tcl_Eval(_interp, str));
99 public string GetVar(string varName, int flags) {
100 unsafe {
101 return Marshal.PtrToStringAnsi(TclNative.Tcl_GetVar(_interp, varName, flags));
105 public string GetVar(string varName) {
106 return GetVar(varName, 0);
109 public TclCmdToken CreateCommand(string cmdName, TclCmdProc proc, TclCmdDeleteProc deleteProc) {
110 TclCmdToken token;
112 TclCmdProcWrapper cmdWrapper = new TclCmdProcWrapper(this, proc);
113 TclCmdDeleteProcWrapper deleteWrapper = new TclCmdDeleteProcWrapper(this, deleteProc, cmdWrapper);
115 unsafe {
116 token._token = TclNative.Tcl_CreateCommand(_interp, cmdName, cmdWrapper.Callback,
117 IntPtr.Zero, deleteWrapper.Callback);
120 return token;
123 public TclCmdToken CreateCommand(string cmdName, TclCmdProc proc) {
124 return CreateCommand(cmdName, proc, null);